package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.StateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdSysStateService;
import com.onluy.sysman.entity.sysmgr.TdSysState;

@Validated
@RestController
@RequestMapping("/tdSysState")
@Api(value="tdSysState", description="/TdSysState管理接口", position=1 )
public class TdSysStateController extends AppWalkController {


	//调用服务
	@Autowired
	private TdSysStateService tdSysStateService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据  areaCode,sysCode,stateType,stateCode  获取信息")
	public RspInfo<TdSysState> dotarget(@Valid @RequestBody ReqInfo<ReqHead, StateParam> reqInfo   ) throws Exception {
		RspInfo<TdSysState> jsonResult = new RspInfo<TdSysState>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdSysState tdSysState = new TdSysState();


		tdSysState.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysState.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysState.setStateType(reqInfo.getReqBody().getStateType()).asCondition();
		tdSysState.setStateCode(reqInfo.getReqBody().getStateCode()).asCondition();


		tdSysState = tdSysStateService.model(tdSysState);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdSysState);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdSysState> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdSysState tdSysState = reqInfo.getReqBody();
		num = tdSysStateService.add(tdSysState);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据  areaCode,sysCode,stateType,stateCode 更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdSysState> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;
		TdSysState tdSysState = reqInfo.getReqBody();

		tdSysState.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysState.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysState.setStateType(reqInfo.getReqBody().getStateType()).asCondition();
		tdSysState.setStateCode(reqInfo.getReqBody().getStateCode()).asCondition();

		num=tdSysStateService.update( tdSysState);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据  id  删除")
	public RspInfo<InParam> doDel(@Valid @RequestBody    ReqInfo<ReqHead,StateParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TdSysState tdSysState = new TdSysState();
		int num=1;
	 

			tdSysState.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
			tdSysState.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
			tdSysState.setStateType(reqInfo.getReqBody().getStateType()).asCondition();
			tdSysState.setStateCode(reqInfo.getReqBody().getStateCode()).asCondition();

			num=tdSysStateService.del( tdSysState);
		 
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="列表明细")
	public RspInfo<PageData<TdSysState>>  doList(@RequestBody    ReqInfo<ParamPagination, TdSysState> reqInfo )throws Exception {
		RspInfo<PageData<TdSysState>> jsonResult=new RspInfo<PageData<TdSysState>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TdSysState> list =tdSysStateService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdSysState tdSysState){

		if(tdSysState == null )return;

		if(null!=tdSysState.getAreaCode()){
			tdSysState.setAreaCode(tdSysState.getAreaCode()).asCondition();
		}if(null!=tdSysState.getSysCode()){
			tdSysState.setSysCode(tdSysState.getSysCode()).asCondition();
		}if(null!=tdSysState.getStateType()){
			tdSysState.setStateType(tdSysState.getStateType()).asCondition();
		}
		if(null!=tdSysState.getStateCode()){
			tdSysState.setStateCode(tdSysState.getStateCode()).asCondition();
		}
		
		if(null!=tdSysState.getStateName()){
			tdSysState.setStateName(null).getCondition().andLike( "%"+tdSysState.getStateName()+"%" );
		}

		if(null!=tdSysState.getStateDesc()){
			tdSysState.setStateDesc(null).getCondition().andLike( "%"+tdSysState.getStateDesc()+"%" );
		}
	}


}
